<template>
	<view class="">
		<image style="width: 750rpx; height: 304rpx;" src="/static/images/tab1/over.png" mode=""></image>
		<view class="w-1 p-30 fl-s-s">
			<text class="c-33 fs-28 bold">请上传服务后照片</text>
			<view class="w-1 fl-s-s m-t-20">
				<up-upload :sizeType="['compressed']" :fileList="fileList1"
					@afterRead="afterRead" @delete="deletePic" name="1" :multiple="true" width="156rpx"
					height="156rpx" :maxCount="6">
					<view class="fl-c br-20" style="width: 156rpx; height: 156rpx; background: #f3f3f3;">
						<image :src="$path + '/tab1/camera.png'" style="width: 51rpx; height: 49rpx;" mode=""></image>
						<text class="fs-20 c-77">上传图片</text>
					</view>
				</up-upload>
			</view>
		</view>
		
		<view class="iphonex">
			<view class="w-1 p-30">
				<view class="big-btn" @click="sub">确定</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		baseUrl
	} from '@/utils/base.js';
	
	const id = ref(0)
	onLoad(o => {
		id.value = o.id;
	})
	
	const completeimg = ref('')
	function sub() {
		if (!completeimg.value) return uni.$u.toast('请上传图片');
		uni.$post({
			url:'/api/Ordersystem/tocourieronorder',
			data:{
				type:5,
				aid:id.value,
				completeimg:completeimg.value
			}
		}).then(res=>{
			getApp().tt(res.msg,'success')
			uni.$emit('reorder')
			uni.$emit('red2')
			uni.$delayBack();
		})
	}
	
	
	const fileList1 = ref([]);
	watch(fileList1, val => {
		console.log('图片val', val)
		let arr = []
		val.map(item => {
			if (item.status == 'success') {
				// arr.push('/uploads/' + item.url.split('/uploads/')[1])
				arr.push(item.url)
			}
		})
		completeimg.value = arr.join(',')
	}, {
		deep: true
	})
	
	
	
	
	// 删除图片
	const deletePic = (event) => {
		const arr = ref([]);
		switch (event.name * 1) {
			case 1:
				arr.value = fileList1.value;
				break;
			case 2:
				arr.value = fileList2.value;
				break;
			case 3:
				arr.value = fileList3.value;
				break;
		}
		arr.value.splice(event.index, 1);
	};
	// 新增图片
	const afterRead = async (event) => {
		const arr = ref([]);
		switch (event.name * 1) {
			case 1:
				arr.value = fileList1.value;
				break;
			case 2:
				arr.value = fileList2.value;
				break;
			case 3:
				arr.value = fileList3.value;
				break;
		}
		// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
		let lists = [].concat(event.file);
		let fileListLen = arr.value.length;
		lists.map((item) => {
			arr.value.push({
				...item,
				status: 'uploading',
				message: '上传中',
			});
		});
		for (let i = 0; i < lists.length; i++) {
			const result = await uploadFilePromise(lists[i].url);
			let item = arr.value[fileListLen];
			arr.value.splice(fileListLen, 1, {
				...item,
				status: 'success',
				message: '',
				url: result,
			});
			fileListLen++;
		}
	};
	
	const uploadFilePromise = (url) => {
		return new Promise((resolve, reject) => {
			let a = uni.uploadFile({
				url: baseUrl + '/api/Common/upload', // 仅为示例，非真实的接口地址
				filePath: url,
				name: 'file',
				header: {
					token: uni.getStorageSync('user').token
				},
				success: (res) => {
					setTimeout(() => {
						resolve(JSON.parse(res.data).data.fullurl)
					}, 1000);
				},
			});
		});
	};
</script>

<style>

</style>
